package com.basis.service;

import com.basis.utils.CheckSQLParamsUtil;
import com.basis.utils.HibernateDao;
import com.mes.data.EmesDcInputData;
import com.mes.data.EmesDcInputDataDetail;
import org.openbravo.dal.service.OBDal;
import java.util.List;
import java.util.Map;

public class DCInputPageService {
	public void parseSqlColumnAndParams(EmesDcInputData ds) {
		CheckSQLParamsUtil util = CheckSQLParamsUtil.getInstance();
		String sql = util.getExeSQL(ds.getSql(), "'1'");
		List<Map<String, Object>> list = HibernateDao.getFields(sql);
		checkColumn(ds, list);
	}

	/**
	 * 检出字段
	 */
	private void checkColumn(EmesDcInputData ds, List<Map<String, Object>> columnList) {
		Map<String, Object> column = null;

		long seq = 10;
		String columnNames = "";
		EmesDcInputDataDetail sourceColumn = null;

		for (Map<String, Object> map : columnList) {
			column = this.getFieldText((String)map.get("name"));
			if (column != null && column.get("name") != null && !"".equals(column.get("name"))) {
				map.put("text", column.get("name"));
			}
			column = this.getFieldByName((String)map.get("name"), ds.getId());
			if (column == null) {
				sourceColumn = new EmesDcInputDataDetail();
				sourceColumn.setColumnName((String)map.get("name"));
				sourceColumn.setName(map.get("text") == null ? (String)map.get("name") : (String)map.get("text"));
				sourceColumn.setSeqNo(seq);
				sourceColumn.setDataType(getColumnType((String)map.get("type"), map.get("fixed")));
				sourceColumn.setEmesDcInputData(ds);

				OBDal.getInstance().save(sourceColumn);
			}
			columnNames += ",'" + map.get("name").toString().toUpperCase() + "'";
			seq += 10;
		}
		//OBDal.getInstance().flush();
		clearColumn(columnNames.substring(1), ds.getId());
	}
	/**
	 * 清除无效列
	 */
	private void clearColumn(String columnNames, String id) {
		String sql = "delete from Emes_DC_Input_Data_Detail where Emes_DC_Input_Data_Id = ? " +
				"  and upper(Column_Name) not in (" + columnNames + ")";
		HibernateDao.executeSql(sql, id);
	}
	/**
	 * 根据字段名获取表表列
	 * @param name
	 * @return
	 */
	private Map<String, Object> getFieldText(String name) {
		String sql = "select * from Emes_DC_Input_Data_Detail where UPPER(Column_name) = ?";
		return HibernateDao.queryUnique(sql, name.toUpperCase());
	}
	
	private Map<String, Object> getFieldByName(String name, String dataSourceId) {
		String sql = "select * from Emes_DC_Input_Data_Detail where UPPER(Column_name) = ? and Emes_DC_Input_Data_id = ?";
		return HibernateDao.queryUnique(sql, name.toUpperCase(), dataSourceId);
	}
	/**
	 * 根据数据中型号生成类型
	 */
	private String getColumnType(String type, Object fixed) {
		if ("INT".equalsIgnoreCase(type) || "LONG".equalsIgnoreCase(type)) {
			return "INT";
		}
		if ("FLOAT".equalsIgnoreCase(type) || "DOUBLE".equalsIgnoreCase(type)) {
			return "FLOAT";
		}
		if ("NUMBER".equalsIgnoreCase(type)) {
			if (fixed != null && Integer.parseInt(fixed.toString()) > 0) {
				return "FLOAT";
			}
			return "INT";
		}
		if ("DATE".equalsIgnoreCase(type) || "DATETIME".equalsIgnoreCase(type)) {
			return "DATE";
		}
		return "STRING";
	}
}